<?php
class Salary_m extends MY_Model{
	public $config_salary;//lay config thâm niên
	function __construct(){
		parent::__construct();
		//config salary
		$this->load->model('settings/settings_m');
		$this->config_salary = $this->settings_m->get_config('erp');
		$this->_table = 'emp_salary_'.date("Y");		
	}
	//lay ngay lam cua thang - nam
	private function get_day($y,$m){
		$this->db->where('year',$y);
		$this->db->where('month',$m);
		$m = $this->db->get('emp_day')->row();
		return $m;
	}
	public function get_many_by($params = array()){
		$d = 1;//ngay lam chuan cua thang
		$y = date("Y");
		if(!empty($params['year'])){
			$y = $params['year'];
			$this->_table = 'emp_salary_'.$params['year'];
			$d = $this->get_day($params['year'],date("m"));
		}
		else{
			$this->_table = 'emp_salary_'.date("Y");
		}
		/*//1 nhan vien xem tat ca cac thang
		if(!empty($params['year']) && empty($params['month'])){
			//$d = $this->get_day($params['year'],$params['month']);
		}*/
		if(!empty($params['month'])){
			$this->db->where('month',$params['month']);
		}
		if(!empty($params['user_id'])){
			$this->db->where('user_id',$params['user_id']);
		}
		//lay theo list room
		if(!empty($params['list_room'])){
			$this->db->where_in('room_id',$params['list_room']);
		}
		else{
			if(!empty($params['room_id'])){
				$this->db->where('room_id',$params['room_id']);
			}
		}
		$this->db->order_by('month','desc');
		/*parent::get_all();
		echo $this->db->last_query();exit;*/
		//echo 2;exit;
		$result = parent::get_all();
		$this->load->helper("function_salary");
		$this->load->model("emp_profile/export_salary_m","export_m");
		foreach($result as $item){
			
			$item->hide = "";
			$f = ($item->factor) / 2;
			if($f == 0.5){// !=1
				$item->hide = "hide";
			}
			//print_r($item);
			$item->salary3 = $item->salary2 - $item->salary;
			//total = //lương hop dong salary2
			//luong tru do nghi 2,3,4 ngay
			//$item->no_data = ($item->date_work == 0) ? true : false;
			$list_idroom = $this->export_m->get_list_idroom(6); // danh sach id phong ban le
			$date = $this->get_day($y,$item->month);
			if(!empty($date))
				if(in_array($item->room_id,$list_idroom)) // kiem tra ngay lam viec cua ban le hay ban si
					$date_work = $date->value2;
				else
					$date_work = $date->value1;
			else
				$date_work = 0;
			if($date_work != 0){
				$item->date_work = $date_work;
				$salary_array  = salary_total((array)$item,$date_work);
				$item->date_l  = $salary_array["date_l"];
				$item->b_value = $salary_array["baohiem"] ;
				$item->dilai = $salary_array["total_dilai"];
				$item->total = round($salary_array["total_thuclanh"],-3);
				$this->load->library('kunkuncache');
				$file_cache = 'emp_profile/profile_'.$item->user_id;
				$item->profile = $this->kunkuncache->get($file_cache);
			}
			else{return "";}
		}
		return $result;
		
	}
	//tinh so tien tham nien 
	private function seniority($start){
		if(date("Y",$start)==1970) return 0;// chua set ngay bat vao dau lam		
		$o = 41022720; // gia tri time 1 nam
		//tinh so nam di lam
		$cur = strtotime(date("Y-m"));//hien tai
		$sta = $start;//thoi gian vao lam int
		$tot = $cur - $sta;
		$seniority = $this->config_salary->seniority;//he so tham nien 1 năm;		
		if((date("Y",$tot) - 1970) >= 2){			
			return $seniority * (int)(date("Y",$tot) - 1970);//so tien tham nien 
		}		
		//$v = $seniority * $ye;//so tien tham nien 
		return 0;
	}
	
	//tao table
	public function create_table($y){
		$tbl_name = 'emp_salary_'.$y;		
		if (!$this->db->table_exists($tbl_name))
		{
				$fields = array(
					'user_id' => array(
						'type' => 'INT',
						'constraint' => '11',
					),
					'month' => array(
						'type' => 'tinyint',
						'constraint' => '2',
					),
					'room_id' => array(
						'type' => 'INT',
						'constraint' => '11',
					),
				);
				$this->dbforge->add_field($fields);
				$this->dbforge->add_field("info text collate utf8_unicode_ci COMMENT 'backup thông tin nhân viên thời diểm hiện tại (JSON)'");
				$this->dbforge->add_field("factor double default NULL COMMENT 'hệ số lương ,thay doi'");
				$this->dbforge->add_field("factor_note varchar(200) collate utf8_unicode_ci default NULL COMMENT 'ghi chú hệ số lương'");
				$this->dbforge->add_field("salary double default NULL COMMENT 'luong cơ bản'");
				$this->dbforge->add_field("salary2 double default NULL COMMENT 'lương hợp đồng : lương phụ cấp (lương hợp đồng - lương cơ bản)'");
				$this->dbforge->add_field("salary_add1 double default NULL COMMENT 'thâm niên'");
				$this->dbforge->add_field("salary_add2 double default NULL COMMENT 'phụ cấp đi lại'");
				$this->dbforge->add_field("salary_add3 double default NULL COMMENT 'đi lại theo công'");
				$this->dbforge->add_field("salary_add4 double default NULL COMMENT 'trách nhiệm'");
				$this->dbforge->add_field("except1 double default NULL COMMENT '- Bù lương'");
				$this->dbforge->add_field("except2 double default NULL COMMENT '- khoản trừ 2'");
				$this->dbforge->add_field("except3 double default NULL COMMENT '- khoản trừ 3'");
				//$this->dbforge->add_field("date_work tinyint(2) NOT NULL default '0' COMMENT 'Ngày làm chuẩn của tháng'");
				$this->dbforge->add_field("date_s tinyint(2) NOT NULL default '0' COMMENT 'Ngày nghi có phép'");
				$this->dbforge->add_field("date_n tinyint(2) NOT NULL default '0' COMMENT 'ngày nghỉ ko phép (ko tính lương)'");
				$this->dbforge->add_field("minute_s smallint(6) NOT NULL default '0' COMMENT 'Số phút nghỉ có phép : 30 ngày = 14.440p'");
				$this->dbforge->add_field("minute_n smallint(6) NOT NULL default '0' COMMENT 'Số phút nghỉ ko phép : #'");
				$this->dbforge->add_field("minute_t smallint(6) NOT NULL default '0' COMMENT 'tổng số phút đi trễ về sớm'");
				$this->dbforge->add_field("notes varchar(300) collate utf8_unicode_ci default NULL COMMENT 'ghi chu'");
				$this->dbforge->add_field("status tinyint(1) NOT NULL default '1' COMMENT '1: da ky hop dong'");
				$this->dbforge->add_field("plus double default NULL COMMENT 'Khoản cộng'");
				$this->dbforge->add_field("plus_note varchar(200) collate utf8_unicode_ci default NULL COMMENT 'ghi chú khoản cộng'");
				$this->dbforge->add_field("minus double default NULL COMMENT 'Khoản trừ'");
				$this->dbforge->add_field("minus_note varchar(200) collate utf8_unicode_ci default NULL COMMENT 'ghi chú khoản trừ'");
				//$this->dbforge->add_field("");
				$this->dbforge->add_key('user_id', TRUE);
				$this->dbforge->add_key('month', TRUE);
				$this->dbforge->add_key('room_id', TRUE);
				$this->dbforge->create_table($tbl_name,TRUE);
				//$this->dbforge->add_column($tbl_name, $fields);
				
		}
	}
	
	public function check_and_insert($m = NULL,$y=""){
		//insert emp_day truoc
		$y = !empty($y)?$y:date("Y");
		if(!empty($m)){
			$this->db->where('year',$y);
			$this->db->where('month',$m);
			$c = $this->db->count_all_results('emp_day');
			if($c == 0){
				$da = array('year'=>date("Y"),'month'=>$m);
				$this->db->insert('emp_day',$da);
			}
		}
		$m = !empty($m) ? $m : date("m");
		$this->_table = 'emp_salary_'.$y;
		$this->db->where('month',$m);
		$count = $this->db->count_all_results($this->_table);
		if($count == 0){
			$profile = $this->profile_m->get_all(array('active'=>1));//lay nhan vien active ma thoi
			$data_add = array();
			$this->load->model('emp_profile/day_m');
			//ngay lam trong thang.nam lay o table  emp_day
			$day_value = $this->day_m->get($y,$m);
			foreach($profile as $item){
				$add['user_id'] = $item->id;
				$add['month'] = $m;
				$add['info'] = json_encode($item);
				$add['factor'] = $item->factor;
				$add['salary'] = $item->salary;
				$add['salary2'] = $item->salary2;
				
				$sta = strtotime(date("Y-m",$item->date_start));//thoi gian vao lam
				$add['salary_add1'] = $this->seniority($sta);//tham nien chua tinh
				$add['salary_add2'] = $item->s1;//phu cap di lai 1 ngay
				$add['salary_add3'] = 0;//di lai theo cong
				$add['salary_add4'] = $item->s2;//+ trach nhiem
				//$add['date_work'] = $day_value->value1;//ngay lam cua cong ty thang do
				$add['date_s'] = 0;//ngay nghi co phep
				$add['date_n'] = 0;//ngay nghi khong phep
				$add['status'] = $item->status;//co bao hiem hay ko 1: co 
				$add['room_id'] = $item->room_id;
				$data_add[] = $add;
			}
			$this->db->insert_batch($this->_table, $data_add); 
		}
	}
	public function update_plus_minus($method,$year,$month,$data){
		$this->_table = 'emp_salary_'.$year;
		$s = array();
		if(!empty($data['value'])){
			$values = $data['value'];
			foreach($values as $key=>$v){
				//$s['user_id'] = $key;
				$s[$method] = $v;
				if(!empty($data['note'])){
					$notes = $data['note'];
					$s[$method.'_note'] = $notes[$key];
				}
				$this->db->where('month',$month);
				$this->db->where('user_id',$key);
				$this->db->update($this->_table,$s);
			}
		}
	}
}